﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Autofac.Core;
using log4net;

namespace PortableVersion.BootStapper
{
    public class LogInjectionModule : Module
    {
        protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
        {
            registration.Preparing += OnComponentPreparing;
        }

        static void OnComponentPreparing(object sender, PreparingEventArgs e)
        {
            var t = e.Component.Activator.LimitType;
            if (e.Parameters.OfType<TypedParameter>().Any(p => p.Type == typeof(ILog)))
                return;
            e.Parameters = e.Parameters.Union(new[]
                                                  {
                                                      new TypedParameter(typeof(ILog),
                                                                            LogManager.GetLogger(t))
                                                  });
        }
    }
}
